package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.EquipmentType;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.EquipmentTypeService;
import com.woniuxy.factory.service.imp.EquipmentTypeServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/equipmentType")
public class EquipmentTypeController {
    @Autowired
    EquipmentTypeService equipmentTypeService;

    @ResponseBody
    @RequestMapping("/show")
    public ResponseResult showEquipmentType() {
        ResponseResult<Object> res = new ResponseResult<>();
        //非空验证
        /*if (page == null || limit == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
        }*/
        //执行查询业务
        //查询所有设备类型
        List<EquipmentType> equipmentTypes = equipmentTypeService.findEquipment();
        //查询设备类型总条数
        Integer count = equipmentTypeService.totalEquipmentType();
        //封装数据
        res.setCode(0);
        res.setMsg("查询成功");
        res.setCount(count);
        res.setData(equipmentTypes);
        return res;
    }

    @ResponseBody
    @RequestMapping("/modify")
    public ResponseResult modifyEquipmentType(EquipmentType equipmentType) {
        ResponseResult<Object> res = new ResponseResult<>();
        Integer tid = equipmentType.getTid();
        String ename = equipmentType.getEname();
        Integer enumber = equipmentType.getEnumber();
        String description = equipmentType.getDescription();
        //非空验证
        if (tid == null || ename == null || ename.isEmpty() || enumber == null || description == null || description.isEmpty()) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentTypeService.modifyEquipmentType(equipmentType);
        res.setCode(0);
        res.setMsg("修改成功");
        return res;
    }

    @ResponseBody
    @RequestMapping("/delet")
    public ResponseResult deletEquipmentById(Integer tid) {
        ResponseResult<Object> res = new ResponseResult<>();
        //非空验证
        if (tid == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentTypeService.deleteEquipmentTypeByTid(tid);
        res.setCode(0);
        res.setMsg("删除成功");
        return res;
    }
    @ResponseBody
    @RequestMapping("/add")
    public ResponseResult addEquipmentType(EquipmentType equipmentType){
        ResponseResult<Object> res = new ResponseResult<>();
        String ename = equipmentType.getEname();
        String description = equipmentType.getDescription();
        //非空验证
        if (ename == null || ename.isEmpty() || description == null || description.isEmpty()) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentTypeService.addEquipmentType(equipmentType);
        res.setCode(0);
        res.setMsg("添加成功");
        return res;
    }
}
